﻿/*
 * Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

#ifndef BURN_H
#define BURN_H
#include "Common.h"

extern "C"
{
#ifdef LINUX
#define LIBBURNFASTBOOT_API
#else
#ifdef BURNFASTBOOT_EXPORTS
#define LIBBURNFASTBOOT_API __declspec(dllexport)
#else
#define LIBBURNFASTBOOT_API __declspec(dllimport)
#endif
#endif
/************************************************************************
* Function: Scan serial.
* Output Params:
*    comName: list of serial port.
*    serialNum: the number of serial port.
* Return Value:
*    0 means success, and -1 mean failed.
************************************************************************/
LIBBURNFASTBOOT_API int GetSerialPortList(char *comName, int len);

/************************************************************************
* Function: open serial.
* Input Params:
*    comName: serial port to be opened.
*    baudRate: serial baud rate to be opened.
*    stopBit: serial stop bit to be opened.
*    parity: serial parity to be opened.
* Return Value:
*    Serial port handle, nonzero means success, and 0 means failed.
************************************************************************/
LIBBURNFASTBOOT_API long long OpenSerialPort(const char *comName, int baudRate, int stopBit, int parity);

/************************************************************************
* Function: open socket.
* Input Params:
*    ip: network ip.
*    port: network port.
*    transportType: 0 means serial net, 1 means udp, 2 means tftp
* Return Value:
*    Network port handle, nonzero means success, and 0 means failed.
************************************************************************/
LIBBURNFASTBOOT_API long long OpenNetwork(const char *ip, int port, int transportType);

/************************************************************************
* Function: get usb device name.
* Input Params:
*    comFd:  usb handle.
* Return Value:
*    usb device name.
************************************************************************/
LIBBURNFASTBOOT_API int GetUsbDeviceName(long long comFd, char *usbDeviceName, int len);

/************************************************************************
* Function: clear all usb number.
* Return Value:
*    no return value
************************************************************************/
LIBBURNFASTBOOT_API void ClearUsbNums();

/************************************************************************
* Function: erase specified  usb number.
* Return Value:
*    no return value
************************************************************************/
LIBBURNFASTBOOT_API void DeleteUsbDeviceName(const char *usbDeviceName);

/************************************************************************
* Function: set spark link pro parameters.
* Input Params:
*    uartBaudRate: uart baud rate.
*    i2cClockSpeed: i2c clock speed.
*    deviceAddress: i2c device address.
*    spiBaudRatePrescaler: spi baud rate prescaler.
*    canBaudRate: can baud rate.
* Return Value:
*    no return value
************************************************************************/
LIBBURNFASTBOOT_API void SetSparkLinkParameters(int uartBaudRate, int i2cClockSpeed, int deviceAddress,
    int spiBaudRatePrescaler, int canBaudRate);

/************************************************************************
* Function: open usb.
* Input Params:
*    vid: usb vid.
*    pid: usb pid.
*    usbNum: usb number.
*    usbNum: usb number
*    transformsType: 0: usb to usb, 1: usb to uart, 2: usb to i2c, 3: usb to spi
*        4: usb to can, 5: usb to jtag, 6: usb to swd
*    timeout: send data timeout. the unit is millseconds.
* Return Value:
*    Usb port handle, nonzero means success, and 0 means failed.
************************************************************************/
LIBBURNFASTBOOT_API long long OpenUsb(int vid, int pid, const char *usbDeviceName,
    Common::UsbTransformsType transformsType, int timeout);

/************************************************************************
* Function: open jtag.
* Input Params:
*    passwordMode: password mode.
*    cfgType: cfg type.
*    password: password.
*    frequency: jtag frequency.
*    port: net port.
*    dllPath: openocd path.
* Return Value:
*    Jtag port handle, nonzero means success, and 0 means failed.
************************************************************************/
LIBBURNFASTBOOT_API long long OpenJtag(int passwordMode, const char *targetCfg, const char *interfaceCfg,
    char *password, int frequency, int port, const char *dllPath);

/************************************************************************
* Function: reset board.
* Input Params:
*    comFd: device handle to be reset.
* Return Value:
*    1 means success, and 0 mean failed.
************************************************************************/
LIBBURNFASTBOOT_API int Reset(long long comFd);

/************************************************************************
* Function: set handshake timeout
* Input Params:
*    comFd: handle to be close.
*    timeout: handshake timeout
* Return Value:
*    no return value
************************************************************************/
LIBBURNFASTBOOT_API void SetHandshakeTimeout(long long comFd, int timeout);

/************************************************************************
* Function: set per packet size
* Input Params:
*    comFd: handle to be close.
*    perPacketSize: size per 1 package
* Return Value:
*    no return value
************************************************************************/
LIBBURNFASTBOOT_API void SetPerPacketSize(long long comFd, int perPacketSize);

/************************************************************************
* Function: set is reset
* Input Params:
*    comFd: handle to be close.
*    isFinal: restart or not
* Return Value:
*    no return value
************************************************************************/
LIBBURNFASTBOOT_API void SetIsReset(long long comFd, int isReset);

/************************************************************************
* Function: set is ready
* Input Params:
*    comFd: handle to be close.
*    isFinal: ready or not
* Return Value:
*    no return value
************************************************************************/
LIBBURNFASTBOOT_API void SetIsReady(long long comFd, int isReady);

/************************************************************************
* Function: close device.
* Input Params:
*    comFd: device handle to be close.
* Return Value:
*    1 means success, and 0 mean failed.
************************************************************************/
LIBBURNFASTBOOT_API int DestroyDevice(long long comFd);

/************************************************************************
* Function: send data command.
* Input Params:
*    comFd: devices handle.
*    data: the data to be send.
*    dataLen: the lenght of the data.
*    recv: recv buffer.
*    recvLen: the lenght of recv.
*    timeout: send data timeout. the unit is millseconds.
* Return Value:
*    0 means failed, and 1 mean success.
************************************************************************/
LIBBURNFASTBOOT_API int SendCmd(long long comFd, unsigned char *data, int dataLen, unsigned char *recevie,
    int recvLen, int timeout);

/************************************************************************
* Function: send data from uart.
* Input Params:
*    comFd: serial port handle to be close.
*    data: the data to be send.
*    dataLen: the lenght of the data.
* Return Value:
*    size of sent data.
************************************************************************/
LIBBURNFASTBOOT_API int SendBytes(long long comFd, unsigned char *data, int dataLen);

/************************************************************************
* Function: recevie command data from uart.
* Input Params:
*    comFd: serial port handle to be close.
*    data: the data to be send.
*    dataLen: the lenght of the data.
* Return Value:
*    the number of received data.
************************************************************************/
LIBBURNFASTBOOT_API int ReceiveBytes(long long comFd, unsigned char *data, int dataLen);

/************************************************************************
* Function: set chiptype.
* Input Params:
*    chipType: high 16 bit represent the department, low 16 bit represent the chipType
************************************************************************/
LIBBURNFASTBOOT_API void SetChipType(unsigned int chipType);

/************************************************************************
* Function: set is not erase before burn.
* Input Params:
*    isNotEraseBeforeBurn: 0: not erase before burn, 1: erase before burn
************************************************************************/
LIBBURNFASTBOOT_API void SetIsEraseBeforeBurn(unsigned int isEraseBeforeBurn);

/************************************************************************
* Function: set address mode.
* Input Params:
*    address mode: only 3 byte address mode and 4 byte address mode
************************************************************************/
LIBBURNFASTBOOT_API void SetAddressMode(unsigned int addressMode);

/************************************************************************
* Function: register the print
* Input Params:
*    printinfo: the callback of printing
*    setProgress: the callback of setProgress
* Return Value:
*    no return value
************************************************************************/
LIBBURNFASTBOOT_API void RegisterCallback(FPPrintInfo printinfo, IsCanceled isCancel, FPSetProgress setProgress);

/************************************************************************
* Function: cancel single thread
* Input Params:
*    cancel: whether it is canceled or not
* Return Value:
*    no return value
************************************************************************/
LIBBURNFASTBOOT_API void Cancel(unsigned long long comFd);

/************************************************************************
* Function: communicate with bootrom until the boot is runnnig
* Input Params:
*    comFd:  handle.
*    filePath: the path of boot file
*    target: 0 means flash,1 means ddr
*    stepPath: step0.bin path.
* Output Params:
*    bootddr: the address of ddr
* Return Value:
*    error number.
************************************************************************/
LIBBURNFASTBOOT_API int BootDownload(unsigned long long comFd, const char *filePath, unsigned long address, int target,
    const char *stepPath, long long *bootddr);

/************************************************************************
* Function: Down load allinone bin file to flash
* Input Params:
*    comFd:  handle.
*    filePath: the path of boot file
* Return Value:
*    0 means failed, and 1 mean success.
************************************************************************/
LIBBURNFASTBOOT_API int DownloadAllinoneBin(unsigned long long comFd, const char *filePath, unsigned long address);

/************************************************************************
* Function: set is only burn loader in DownloadAllinoneBin.
* Input Params:
*    isOnlyBurnLoader: 0: burn all file, 1: only burn loader file
************************************************************************/
LIBBURNFASTBOOT_API void SetIsOnlyBurnLoader(unsigned int isOnlyBurnLoader);

/************************************************************************
* Function: Erase flash
* Input Params:
*    comFd:  handle.
*    filePath: the path of boot file
*    address: erase address
*    length: erase length
* Return Value:
*    0 means failed, and 1 mean success.
************************************************************************/
LIBBURNFASTBOOT_API int EraseFlash(unsigned long long comFd, unsigned long address, unsigned long length);

/************************************************************************
* Function: Down load touch bin file to flash
* Input Params:
*    comFd:  handle.
*    touchBin: the path of touch file
*    imageBin: the path of image file
*    vctmBin: the path of vctm file
*    moveFlashWays: 0 means move flash by two, 1 means move flash by four
* Return Value:
*    0 means failed, and 1 mean success.
************************************************************************/
LIBBURNFASTBOOT_API int DownloadTouchBin(unsigned long long comFd, const char *touchBin, const char *imageBin,
    const char *vctmBin, int moveFlashWays);

/************************************************************************
* Function: Down load to ddr.
* Input Params:
*    comFd:  handle to be close.
*    filePath: the path of boot file
*    data: the data to be send.
*    dataLen: the lenght of the data.
*    burnAddress: burn address
* Return Value:
*    0 means failed, and 1 mean success.
************************************************************************/
LIBBURNFASTBOOT_API int DownloadToDdr(long long comFd, const char *filePath, unsigned char *data, unsigned long dataLen,
    unsigned long burnAddress);

/************************************************************************
* Function: split file
* Input Params:
*    fileName: file name.
*    fileSystem: file system.
*    address: address.
*    length: length.
*    page: page.
*    oob: oob.
*    splitSize: split file size
* Return Value:
*    0 means failed, and 1 mean success.
************************************************************************/
LIBBURNFASTBOOT_API char* SplitFile(const char *fileName, const char *fileSystem, unsigned long address,
    unsigned long length, unsigned long page, unsigned long oob, unsigned long splitSize);

/************************************************************************
* Function: Down load to flash.
* Input Params:
*    comFd:  handle to be close.
*    filePath: file path.
*    flashType: flash type
*    fileSystem: file system
*    ddrAddress: ddr address
*    length: length
*    packetSize: packet size
* Return Value:
*    0 means failed, and 1 mean success.
************************************************************************/
LIBBURNFASTBOOT_API int DownloadToFlash(long long comFd, const char *filePath, const char *flashType,
    const char *fileSystem, unsigned long ddrAddress, unsigned long length, int packetSize, int fileType);

/************************************************************************
* Function: upload file.
* Input Params:
*    comFd: handle to be close.
*    address: boot ddr addr.
*    length: uplaod length.
*    filePath: upload file path.
* Return Value:
*    0 means failed, and 1 mean success.
************************************************************************/
LIBBURNFASTBOOT_API int UploadFile(long long comFd, unsigned long address, int length, const char *filePath);

/************************************************************************
* Function: read flash to file.
* Input Params:
*    comFd: handle to be close.
*    flashBlock: flash block.
*    filePath: upload file path.
*    blockOffset: block offset.
*    length: read flash length.
* Return Value:
*    0 means failed, and 1 mean success.
************************************************************************/
LIBBURNFASTBOOT_API int ReadFlashToFile(long long comFd, unsigned long flashBlock, const char *filePath,
    unsigned long blockOffset, unsigned long length);

/************************************************************************
* Function: read register
* Input Params:
*    comFd: handle to be close.
*    data: the data to be read.
*    dataLen: the lenght of the data.
*    address: register address.
* Return Value:
*    0 means failed, and 1 mean success.
************************************************************************/
LIBBURNFASTBOOT_API int ReadRegister(long long comFd, int deviceAddress, unsigned char *data, int dataLen,
    unsigned long address);

/************************************************************************
* Function: write register
* Input Params:
*    comFd: handle to be close.
*    data: the data to be write.
*    dataLen: the lenght of the data.
*    address: register address.
* Return Value:
*    0 means failed, and 1 mean success.
************************************************************************/
LIBBURNFASTBOOT_API int WriteRegister(long long comFd, int deviceAddress, unsigned char *data, int dataLen,
    unsigned long address);

/************************************************************************
* Function: I2C debug mode switch
* Input Params:
*    comFd: handle to be close.
*    type: 0 means enter i2c debug mode, 1 means exit i2c debug mode
* Return Value:
*    0 means failed, and 1 mean success.
************************************************************************/
LIBBURNFASTBOOT_API int I2CDebugModeSwitch(long long comFd, int type, int deviceAddress);

/************************************************************************
* Function: obtaining version information
* Input Params:
*       device，const:device  handle.
* Output Params:
*       version: version information
* Return Value:
*       TRUE means success, and FALSE mean failed.
************************************************************************/
LIBBURNFASTBOOT_API int GetVersionInfo(unsigned long long device, int deviceAddress, char *version);

/************************************************************************
* Function: I2C Upgrede
* Input Params:
*    device: handle to be close.
*    strFilePath: upgrede file path.
*    deviceAddress: device address.
*    queryInterval: upgrede query interval,the unit is millseconds.
*    queryTimes: upgrede query times.
* Return Value:
*    0 means failed, and 1 mean success.
************************************************************************/
LIBBURNFASTBOOT_API int I2CUpgrade(unsigned long long device, const char *strFilePath, int deviceAddress,
    int queryInterval, int queryTimes);

/************************************************************************
* Function: write message
* Input Params:
*    comFd: handle to be close.
*    sendData: the data to be send.
*    sendDataSize: the size of the send data.
*    recvData: the data to be recv.
*    recvDataSize: the size of the recv data.
*    magicNum: read magicNum.
*    cmd: read cmd.
* Return Value:
*    0 means failed, and 1 mean success.
************************************************************************/
LIBBURNFASTBOOT_API int ReadMessage(long long comFd, unsigned char* sendData, int sendDataSize,
    unsigned char* recvData, int recvDataSize, int magicNum, short cmd);

/************************************************************************
* Function: write message
* Input Params:
*    comFd: handle to be close.
*    data: the data to be write.
*    dataSize: the size of the data.
*    magicNum: write magicNum.
*    cmd: write cmd.
* Return Value:
*    0 means failed, and 1 mean success.
************************************************************************/
LIBBURNFASTBOOT_API int WriteMessage(long long comFd, unsigned char* data, int dataSize, int magicNum, short cmd);

/************************************************************************
* Function: erase all flash
* Input Params:
*    comFd: handle to be close.
*    filePath: file path
* Return Value:
*    no return value
************************************************************************/
LIBBURNFASTBOOT_API int EraseAllFlash(unsigned long long comFd, const char *filePath);

/************************************************************************
* Function: Download smart home boot until the boot is runnnig
* Input Params:
*    comFd: handle.
*    fastbootFilePath: the path of boot file
*    esbcFilePath: the path of ecbc file
*    ddrInitFilePath: the path of ddr init file
* Return Value:
*    0 means failed, and 1 mean success.
************************************************************************/
LIBBURNFASTBOOT_API int DownloadSmartHomeBoot(unsigned long long comFd, const char *fastbootFilePath,
    const char *esbcFilePath, const char *ddrInitFilePath);

/************************************************************************
* Function: Download smart home uboot until the uboot is runnnig
* Input Params:
*    comFd: handle.
*    fastbootFilePath: the path of boot file
* Return Value:
*    0 means failed, and 1 mean success.
************************************************************************/
LIBBURNFASTBOOT_API int RefreshSmartHomeUboot(unsigned long long comFd, const char *fastbootFilePath);

/************************************************************************
* Function: Start TFTP Server
* Input Params:
*    comFd: handle.
*    ip: tftp server ip
*    port: tftp server port
* Return Value:
*    0 means failed, and 1 mean success, and 2 means port is in use.
************************************************************************/
LIBBURNFASTBOOT_API int StartTftpServer(unsigned long long comFd, const char *ip, int port);

/************************************************************************
* Function: Stop TFTP Server
* Input Params:
*    comFd: handle.
* Return Value:
*    0 means failed, and 1 mean success.
************************************************************************/
LIBBURNFASTBOOT_API int CloseTftpServer(unsigned long long comFd);

/************************************************************************
* Function: Download file to flash partition
* Input Params:
*    comFd: handle.
*    filePath: file to download
*    flashType: flash partition to be written
*    ddrAddress: download ddr address
* Return Value:
*    0 means failed, and 1 mean success.
************************************************************************/
LIBBURNFASTBOOT_API int DownloadFileToFlashPartition(unsigned long long comFd, const char *filePath,
    int flashType, unsigned long ddrAddress);

/************************************************************************
* Function: open usb and serial port.
* Input Params:
*    vid: usb vid.
*    pid: usb pid.
*    comName: serial port to be opened.
* Return Value:
*    handle, nonzero means success, and 0 means failed.
************************************************************************/
LIBBURNFASTBOOT_API long long OpenUsbAndSerialPort(int vid, int pid, const char *usbDeviceName,
    const char *comName, int timeout);
}
#endif
